package com.example.smartwaterapi.common.api;

import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 统一API响应结果
 */
@Data
@ToString
public class ApiResponse<T> {
    
    /**
     * 返回消息
     */
    private String message;
    
    /**
     * 返回数据
     */
    private T data;
    
    /**
     * 状态码
     */
    private String code;
    
    /**
     * 时间戳
     */
    private String timestamp;
    
    /**
     * 操作结果
     */
    private boolean success;
    
    /**
     * 请求路径
     */
    private String path;

    /**
     * 默认构造函数
     */
    public ApiResponse() {
        this.timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 根据结果构造
     *
     * @param success 操作结果
     */
    public ApiResponse(boolean success) {
        this();
        this.success = success;
        if (success) {
            this.code = "200000";
            this.message = "操作成功";
        } else {
            this.code = "500000";
            this.message = "操作失败";
        }
    }

    /**
     * 完整构造函数
     *
     * @param success 操作结果
     * @param code 状态码
     * @param message 消息
     * @param data 数据
     */
    public ApiResponse(boolean success, String code, String message, T data) {
        this();
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回数据
     *
     * @param data 数据
     */
    public ApiResponse(T data) {
        this();
        this.success = true;
        this.message = "操作成功";
        this.code = "200000";
        this.data = data;
    }

    // ============ 静态工厂方法 ============

    /**
     * 成功返回
     */
    public static <T> ApiResponse<T> success() {
        return new ApiResponse<>(true);
    }

    /**
     * 成功返回数据
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(data);
    }

    /**
     * 成功返回消息和数据
     */
    public static <T> ApiResponse<T> success(String message, T data) {
        ApiResponse<T> response = new ApiResponse<>(true);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    /**
     * 失败返回
     */
    public static <T> ApiResponse<T> error() {
        return new ApiResponse<>(false);
    }

    /**
     * 失败返回消息
     */
    public static <T> ApiResponse<T> error(String message) {
        ApiResponse<T> response = new ApiResponse<>(false);
        response.setMessage(message);
        return response;
    }

    /**
     * 失败返回消息和数据
     */
    public static <T> ApiResponse<T> error(String message, T data) {
        ApiResponse<T> response = new ApiResponse<>(false);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    /**
     * 指定错误码和消息的错误返回
     */
    public static <T> ApiResponse<T> error(String code, String message) {
        ApiResponse<T> response = new ApiResponse<>(false);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 指定错误码、消息和数据的错误返回
     */
    public static <T> ApiResponse<T> error(String code, String message, T data) {
        ApiResponse<T> response = new ApiResponse<>(false);
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    /**
     * 设置请求路径
     */
    public ApiResponse<T> setPath(String path) {
        this.path = path;
        return this;
    }

    // ============ 链式调用支持 ============

    public ApiResponse<T> withCode(String code) {
        this.code = code;
        return this;
    }

    public ApiResponse<T> withData(T data) {
        this.data = data;
        return this;
    }

    public ApiResponse<T> withMessage(String message) {
        this.message = message;
        return this;
    }

    public ApiResponse<T> withSuccess(boolean success) {
        this.success = success;
        // 自动设置默认状态码和消息
        if (success) {
            if (this.code == null || this.code.startsWith("5")) {
                this.code = "200000";
            }
            if (this.message == null || "操作失败".equals(this.message)) {
                this.message = "操作成功";
            }
        } else {
            if (this.code == null || this.code.startsWith("2")) {
                this.code = "500000";
            }
            if (this.message == null || "操作成功".equals(this.message)) {
                this.message = "操作失败";
            }
        }
        return this;
    }
} 